home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
dirut
/
chmod2.zip
/
CHMOD.C
next >
Wrap
Text File
|
1987-01-15
|
5KB
|
204 lines
/*//////////////////////////////////////////////////////////////////////////
// c h m o d . c
////////////////
CHMOD(1) MS DOS USER COMMANDS CHMOD(1)
NAME
chmod
SYNOPSIS
chmod [=+-][r][h][s][a] file ...
DESCRIPTION
Changes the attributes of 'file'. The file attributes can either
be set (=), or attributes can be added (+) or removed (-) from the
the current attribute. If no attributes are specified, then the file
is marked 'normal' i.e. no attributes set except the archive bit.
Newman Lab cohler@tcgould.tn.cornell.edu
Cornell Univ jbvy@cornella.bitnet
Ithaca NY 14853
I retain all rights to this program. (c) Gene Cohler, January 1987
**************************************************************************/
/*
* N.B. I use tab set to 4
*
* History :
*
* Feb 14 1986 - Version 1 peeks out the window ...
*
* Modified
* :- August 1 1986
* 1) Looks more like unix version - choose to set attribute
* = or - or + to set or remove or add.
* 2) Talks a little more .. old age maybe !
* :- January 1987
* 1) Default works a little better - now Archive set is default.
* Also chmod file will set the archive bit only.
* 2) Ready to hit the streets with v 1.0
* 3) Directory Bugfix made, +- didnt work only =. Fixed.
*****************************************************************************
* Written in Microsoft C v 4.0. Should be linked together with ssetargv.obj
* included in the standard library
*****************************************************************************/
#include <stdio.h>
#include <dos.h>
#define SET 1
#define GET 0
#define SUBDIR 0x10
#define ARCHIVE 0x20
main(argc,argv)
int argc;
char **argv;
{
unsigned attrib = ARCHIVE ;
unsigned oldattrib=0 ;
unsigned newattrib=0 ;
static int what='=' ;
register unsigned i ;
register unsigned notfile ;
if( argc == 1 )
error("No file specified");
/*
* Find what attributes are being called for
*/
for(i=1;i<argc;i++)
{
switch( argv[i][0] )
{
case '-' :
case '+' :
case '=' :
parse(&attrib, argv[i], &what);
notfile = i ;
}
}
/*
* Now set em one by one
*/
for( i=1; i<argc ; i++)
{
if( i != notfile )
{
oldattrib = mode( argv[i], attrib, GET) ;
/*
* Directory attrib not settable
*/
if (oldattrib & SUBDIR)
oldattrib = (oldattrib & (~SUBDIR) ) & 0xff ;
switch( what )
{
case '=' :
newattrib = attrib ;
break;
case '+' :
if(oldattrib == -1)
{
fprintf(stderr,"Error finding mode of %s\n",argv[i]);
continue ;
}
newattrib = oldattrib | attrib ;
break ;
case '-' :
if(oldattrib == -1)
{
fprintf(stderr,"Error finding mode of %s\n",argv[i]);
continue ;
}
newattrib = (oldattrib & (~attrib)) & 0xff;
break;
default :
error("No mode qualifier given");
}
if((oldattrib = mode( argv[i], newattrib, SET)) == -1)
{
fprintf(stderr,"Error setting mode of %s\n",argv[i]);
continue ;
}
}
}
}
/********************************************
* mode.c - change/get the mode - DOS call *
* action is 1 to set the mode :
* action is 0 to get the mode ;
*****************************************/
#define SYSCALL 0x21
int
mode(name, attrib, action )
unsigned char name[] ;
unsigned attrib ;
int action ;
{
struct SREGS segregs ;
union REGS regs ;
if( action == SET) regs.x.ax = 0x4301 ;/* Set up for a chmod */
else regs.x.ax = 0x4300 ;
regs.x.cx = attrib ;
segread(&segregs) ; /* get current ds value */
regs.x.dx = (unsigned int) name; /* name is in ds:dx */
regs.x.cflag = 0 ; /* Make sure flag is zeroed */
int86x(SYSCALL, ®s, ®s, &segregs) ; /* Do it */
if (regs.x.cflag) return (-1) ; /* Return error */
else return (regs.x.cx); /* Return attrib */
}
/*
* Attributes used in the file descriptor
*/
#define NORMAL 0x00
#define READONLY 0x01
#define HIDDEN 0x02
#define SYSTEM 0x04
#define VOLUME 0x08
parse( attrib, arg, what)
unsigned *attrib ;
char arg[] ;
int *what ;
{
register i ;
unsigned numargs = strlen(arg) ;
*attrib = NORMAL ;
*what = arg[0] ;
if (numargs == 1) /* Set Archive as Default */
{
*attrib = ARCHIVE ;
return ;
}
for(i=1; i<numargs ; i++)
{
if( toupper(arg[i]) == 'R' ) *attrib |= READONLY ;
if( toupper(arg[i]) == 'S' ) *attrib |= SYSTEM ;
if( toupper(arg[i]) == 'A' ) *attrib |= ARCHIVE ;
if( toupper(arg[i]) == 'H' ) *attrib |= HIDDEN ;
}
}
error(msg)
char *msg;
{
fprintf(stderr,"Error: %s\n", msg);
fprintf(stderr,"Usage: chmod [+-=][rhsa] file(s)");
exit(1) ;
}